﻿using UnityEngine;
using System.Collections;

public class BattleResultPopup : MonoBehaviour {
	public UILabel legionExpLabel;
	public UILabel legionExpLabel2;
	public UILabel regionNameLabel;
	public UILabel regionExpLabel;

    public UILabel winPointLabel;

	public UISprite bonusItemImage;
	public UISprite bonusItemWindow;
	public UISprite bonusItemGlow;
	public GameObject[] bonusItemRare;
	public UILabel bonusItemName;

	public UISprite bonusItemImage2;
	public UISprite bonusItemWindow2;
	public UISprite bonusItemGlow2;
	public GameObject[] bonusItemRare2;
	public UILabel bonusItemName2;

	public GameObject addFriendObject;
	public GameObject friendRewardObject;
	public UILabel addFriendLabel;
	public UILabel friendRewardLabel;
	public GameObject addFriendButtonObject;

	public GameObject bonusItemObeject;
	public GameObject bonusKinaObeject;
	public GameObject bonusBongObeject;
	public GameObject bonusItemObeject2;
	public GameObject bonusKinaObeject2;
	public GameObject bonusBongObeject2;

	public UILabel kinaAmountLabel;
	public UILabel bongAmountLabel;
	public UILabel kinaAmountLabel2;
	public UILabel bongAmountLabel2;

	public UILabel bonusItemTitleLabel;
	public UILabel bonusItemTitleLabel2;

	public GameObject bonusItemTextObject;
	public GameObject bonusItemTextObject2;
	public GameObject noBonusItemObject;
	public GameObject noBonusItemObject2;

    public GameObject winPointLabelObject;

	// Use this for initialization
	void Start () {
		if( UserData.Instance.isQuickPVPBattle )
		{
			if( bonusItemTitleLabel != null )
				bonusItemTitleLabel.text = TextController.GetStringWithId("uiRewardCaution2");
			if( bonusItemTitleLabel2 != null )
				bonusItemTitleLabel2.text = TextController.GetStringWithId("uiRewardCaution2");
		}
		else
		{
			if( bonusItemTitleLabel != null )
				bonusItemTitleLabel.text = TextController.GetStringWithId("uiRewardBonusItem");
			if( bonusItemTitleLabel2 != null )
				bonusItemTitleLabel2.text = TextController.GetStringWithId("uiRewardBonusItem");
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void OnClickAddFriend()
	{
		NetworkManager.Instance.C2S_FRIEND_SEND_REQ(UserData.Instance.battleFriendId, (responseData) => {
			if (responseData.ecode != 0)
			{
				// TODO: 친구 추가 후 에러 발생시 처리
				if( responseData.ecode != 0 )
				{
					Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
					po.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextwarning"), TextController.GetStringWithId(string.Format("systemErrorText{0}", Mathf.Abs(responseData.ecode))));
					return;
				}
			}
			else
			{
				// TODO: 친구 추가 이후 결과 처리
				addFriendButtonObject.SetActive(false);
				addFriendLabel.text = TextController.GetStringWithId("uiFriendBattleSend").Replace("{0}", string.Format("Lv {0}", UserData.Instance.battleFriend.lv.ToString())).Replace("{1}", UserData.Instance.battleFriend.name);
			}
		});
	}

	public void OnClickRetryButton()
	{
		NetworkManager.Instance.C2S_LEGION_GET(ResponseGetLegionInfoFormResearch);
	}

	int prevRegionLevel = 1;
	public void OnClickCloseButton()
	{
		Debug.LogError("OnClickCloseButton");
		if( WorldSceneManager.isSentSearch )
		{
			prevRegionLevel = UserData.Instance.userModel.regions.regionMap_Dic[WorldSceneManager.sentResearchAreaId.ToString()].regionLevel;
		}
		NetworkManager.Instance.C2S_LEGION_GET(ResponseGetLegionInfo);
	}

	public void ResponseGetLegionInfoFormResearch(ResponseModel<UserModel> result)
	{
		if( result.ecode != 0 )
		{
			Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
			po.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextwarning"), TextController.GetStringWithId(string.Format("systemErrorText{0}", Mathf.Abs(result.ecode))));
			return;
		}
		UserData.Instance.userModel = result.data;
		StandbyScene.startMode = BattleStartMode.Research;
		BattleManager.Instance.ClearGame();
		SceneController.LoadLevel("Lobby_4vs4");
	}

	public void ResponseGetLegionInfo(ResponseModel<UserModel> result)
	{
		if( result.ecode != 0 )
		{
			Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
			po.SetPopupWithTitleAndBody(TextController.GetStringWithId("uiCommonTextwarning"), TextController.GetStringWithId(string.Format("systemErrorText{0}", Mathf.Abs(result.ecode))));
			return;
		}

		int prevExpansionWarId = UserData.Instance.userModel.regions.expansionWarId;
		UserData.Instance.userModel = result.data;

		if( WorldSceneManager.isSentSearch )
		{
			if( prevRegionLevel != UserData.Instance.userModel.regions.regionMap_Dic[WorldSceneManager.sentResearchAreaId.ToString()].regionLevel )
				WorldSceneManager.isRegionLevelUp = true;
		}

		gameObject.SetActive(false);
		if( UserData.Instance.researchResultData != null )
		{
			BattleManager.Instance.ClearGame();

			if( UserData.Instance.researchResultData.dicoverBoss && UserData.Instance.raidBossResultData == null )
			{
				UserData.Instance.raidBossId = UserData.Instance.researchResultData.discoverBossId;
				SceneController.LoadLevel("BossWarning");
			}
			else
			{
				UserData.Instance.raidBossId = 0;
				UserData.Instance.raidBossStartData = null;
				UserData.Instance.raidBossResultData = null;
				SceneController.LoadLevel("World");
			}
		}
		else if( WorldExtensionBattleStageScene.isExpansionWar && UserData.Instance.userModel.regions.expansionWarId == 0 )
		{
			BattleManager.Instance.ClearGame();

			WorldExtensionBattleStageScene.isExpansionWar = false;
			WorldSceneManager.newOpenAreaId = prevExpansionWarId;
			SceneController.LoadLevel("World");
		}
		else if( WorldExtensionBattleStageScene.isExpansionWar && UserData.Instance.userModel.regions.expansionWarId != 0 )
		{
			BattleManager.Instance.ClearGame();

			WorldExtensionBattleStageScene.isExpansionWar = false;
			SceneController.LoadLevel("Expansion");
		}
		else if( UserData.Instance.raidBossId != 0 && UserData.Instance.raidBossResultData != null )
		{
			UserData.Instance.raidBossId = 0;
			UserData.Instance.raidBossStartData = null;
			UserData.Instance.raidBossResultData = null;
			SoundController.PlayBGM("BGM_Main");
			SceneController.LoadLevel("BossList");
		}
		else
		{
            if( BattleManager.Instance != null )
			    BattleManager.Instance.ClearGame();

			WorldExtensionBattleStageScene.isExpansionWar = false;
			UserData.Instance.raidBossModel = null;
            //(지스타 임시)
			SceneController.LoadLevel("BossList");
		}
	}
}
